<template>
	<el-dialog v-model="visible" title="编辑" :close-on-click-modal="false" draggable width="65%">
		<el-form ref="dataFormRef" :model="dataForm" :rules="dataRules" label-width="120px">
			<el-row>
				<el-col :span="12">
					<el-form-item label="项目" prop="projectId">
						<el-select v-model="dataForm.projectId" style="width: 100%" placeholder="请选择项目" disabled>
							<el-option v-for="projectItem in projectList" :key="projectItem.id" :label="projectItem.projectName" :value="projectItem.id">
								<span style="font-weight: bold">{{ projectItem.projectName }}</span>
								<span v-if="projectItem.projectDesc && projectItem.projectDesc.trim()" style="color: #999; font-size: 12px"
									>（{{ projectItem.projectDesc }}）</span
								>
							</el-option>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="表名" prop="tableName">
						<el-input v-model="dataForm.tableName" disabled placeholder="表名"></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row>
				<el-col :span="12">
					<el-form-item label="库名" prop="databaseName">
						<el-input v-model="dataForm.databaseName" disabled placeholder="库名"></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item prop="tableTemplateGroupId" label="表模板组">
						<el-select v-model="dataForm.tableTemplateGroupId" placeholder="请选择表模板组" style="width: 100%" disabled>
							<el-option v-for="item in templateGroupList" :key="item.id" :label="item.groupName" :value="item.id">
								<span style="font-weight: bold">{{ item.groupName }}</span>
								<span v-if="item.groupDesc && item.groupDesc.trim()" style="color: #999; font-size: 12px">（{{ item.groupDesc }}）</span>
							</el-option>
						</el-select>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row>
				<el-col :span="12">
					<el-form-item label="类名" prop="className">
						<el-input v-model="dataForm.className" placeholder="请输入类名" clearable></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="注释" prop="tableComment">
						<el-input v-model="dataForm.tableComment" placeholder="请输入注释" clearable></el-input>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row>
				<el-col :span="12">
					<el-form-item label="功能名" prop="functionName">
						<el-input v-model="dataForm.functionName" placeholder="请输入功能名" clearable></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="表单布局" prop="formLayout">
						<el-radio-group v-model="dataForm.formLayout">
							<el-radio :label="1">一列</el-radio>
							<el-radio :label="2">两列</el-radio>
						</el-radio-group>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row>
				<el-col :span="12">
					<el-form-item label="作者" prop="author">
						<el-input v-model="dataForm.author" placeholder="请输入作者" clearable></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="版本号" prop="version">
						<el-input v-model="dataForm.version" placeholder="请输入版本号" clearable></el-input>
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取消</el-button>
			<el-button type="primary" @click="submitHandle()">确认</el-button>
		</template>
	</el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus/es'
import { useTableApi, useTableSubmitApi } from '@/api/table'
import { templateGroupListApi } from '@/api/template'
import { useProjectListApi } from '@/api/project'

const emit = defineEmits(['refreshDataList'])

const visible = ref(false)
const dataFormRef = ref()
const templateGroupList = ref<any[]>([])
const projectList = ref<any[]>([])
const dataForm = reactive({
	id: null,
	projectId: null,
	tableName: null,
	databaseName: null,
	tableTemplateGroupId: null,
	className: '',
	tableComment: '',
	functionName: '',
	formLayout: null,
	author: '',
	version: ''
})

const init = (id: number) => {
	visible.value = true
	dataForm.id = null

	// 重置表单数据
	if (dataFormRef.value) {
		dataFormRef.value.resetFields()
	}

	getTemplateGroupList()
	getProjectList()
	getTable(id)
}

const getProjectList = () => {
	useProjectListApi().then(res => {
		projectList.value = res.data
	})
}

const getTemplateGroupList = () => {
	templateGroupListApi().then(res => {
		templateGroupList.value = res.data
	})
}

const getTable = (id: number) => {
	useTableApi(id).then(res => {
		Object.assign(dataForm, res.data)
		console.log(res.data)
	})
}

const dataRules = ref({
	tableComment: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	className: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	packageName: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	moduleName: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	functionName: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	formLayout: [{ required: true, message: '必填项不能为空', trigger: 'blur' }]
})

// 保存
const submitHandle = () => {
	dataFormRef.value.validate((valid: boolean) => {
		if (!valid) {
			return false
		}

		useTableSubmitApi(dataForm).then(() => {
			ElMessage.success({
				message: '操作成功',
				duration: 500,
				onClose: () => {
					visible.value = false
					emit('refreshDataList')
				}
			})
		})
	})
}

defineExpose({
	init
})
</script>

<style lang="scss" scoped>
.generator-code .el-dialog__body {
	padding: 15px 30px 0 20px;
}
</style>
